<?php
// Start the session
@session_start();

$id = session_id();
if ($id == "") {
	exit;
}

// If does not exist folder do nothing
$tmpDataFolder = "../tmp_data/" . $id;
if (!file_exists($tmpDataFolder)) {
	exit();
}

$algorithmType = "p";
if (isset($_GET["algorithmType"])) {
	$algorithmType = $_GET["algorithmType"];
}

$limit = 0.75;
if (isset($_GET["percent"])) {
	$limit = floatval($_GET["percent"]);
}

$configXML = new DOMDocument();
$settingsElement = $configXML->createElement("settings");
$minimumMatchingScoreElement = $configXML->createElement("minimum_match_score");
$textNode = $configXML->createTextNode($limit);
$minimumMatchingScoreElement->appendChild($textNode);
$settingsElement->appendChild($minimumMatchingScoreElement);
$configXML->appendChild($settingsElement);
$configXML->save(realpath(dirname(__FILE__) . "/../tmp_data/" . $id) . "/config.xml");

// Execute detect face
exec(realpath(dirname(__FILE__) . "/../../compareFaces/compareFaces") . " " . realpath(dirname(__FILE__) . "/../tmp_data/" . $id) . " " . $algorithmType, $output);
// Create result
$result = array();

foreach($output as $element) {
	// Split line from output
	$lineElements = explode(":", $element);
	
	// Create file name
	$fileId = $lineElements[0];
	$fileName = "data/" . $fileId . ".jpg";
	
	// Add distance
	$distance = $lineElements[1];
	
	$result[$fileName] = $distance;
}
asort($result);
echo json_encode($result);

?>